﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Manager_System
{
    public partial class add_plain : Form
    {
        string type = "";
        
        string Id = "";
        main_form form1;
        public add_plain(main_form form)
        {
            type = "添加";
            form1 = form;
            InitializeComponent();
            plain_tb.MaxLength = 100;
            plain_dt.Enabled = true;
        }
        public add_plain(string id,string plain,main_form form)
        {
            InitializeComponent();
            type = "修改";
            plain_tb.Text = plain;
            Id = id.Substring(0,4)+"/"+id.Substring(4,2)+"/"+id.Substring(6,2);
            plain_dt.Value = Convert.ToDateTime(Id);
            form1 = form;
            plain_tb.MaxLength = 100;
            plain_dt.Enabled = false;
        }

        private void plain_tb_TextChanged(object sender, EventArgs e)
        {
            text_num.Text = "您还可以输入"+(100 - plain_tb.Text.Length).ToString() + "个汉字";
        }

        private void confirm_bt_Click(object sender, EventArgs e)
        {
            #region 添加/修改计划
            string sql = "";
            string ID = plain_dt.Text.Substring(0, 4) + plain_dt.Text.Substring(5, 2) + plain_dt.Text.Substring(8, 2);
            if (type.Equals("添加"))
            {
                sql = "insert into T_plain (ID,Plain) values('" + ID + "','" + this.plain_tb.Text + "')";
            }
            if(type.Equals("修改"))
            {
                sql = "update T_plain set Plain='"+this.plain_tb.Text+"'where ID='"+ID+"'";   
            }
               
                if (plain_tb.Text.Trim() != String.Empty)
                {
                    DBhelp db = new DBhelp();
                    int rs = db.update(sql);
                    if (rs == 1)
                    {
                        MessageBox.Show(type.ToString()+"成功！");
                    }
                    else
                    {
                        MessageBox.Show(type.ToString() + "失败！");
                    }
                    db.close();
                    form1.Refresh_plain_LV();
                }
                else
                {
                    MessageBox.Show("请输入计划！");
                }
            
            #endregion

        }

        private void add_plain_Load(object sender, EventArgs e)
        {
            plain_dt.Format = DateTimePickerFormat.Custom; //设置为显示格式为自定义
            plain_dt.CustomFormat = "yyyy-MM-dd"; 
        }
    }
}
